package com.suning.app.snpaasadmin.operation;

import com.suning.app.snpaasadmin.dao.AppEnvironmentMapper;
import com.suning.app.snpaasadmin.dao.CfAppMapper;
import com.suning.app.snpaasadmin.dao.CfRuntimeMapper;
import com.suning.app.snpaasadmin.dao.CfSdkMapper;
import com.suning.app.snpaasadmin.dao.CfServiceBoughtMapper;
import com.suning.app.snpaasadmin.dao.CfServiceMapper;
import com.suning.app.snpaasadmin.dao.CfUserExternalMapper;
import com.suning.app.snpaasadmin.dao.CfUserRecordMapper;
import com.suning.app.snpaasadmin.domain.ServiceImgInfo;
import com.suning.app.snpaasadmin.domain.UserInfo;
import com.suning.app.snpaasadmin.pojo.AppEnvironment;
import com.suning.app.snpaasadmin.pojo.AppEnvironmentExample;
import com.suning.app.snpaasadmin.pojo.CfApp;
import com.suning.app.snpaasadmin.pojo.CfAppExample;
import com.suning.app.snpaasadmin.pojo.CfRuntime;
import com.suning.app.snpaasadmin.pojo.CfSdk;
import com.suning.app.snpaasadmin.pojo.CfService;
import com.suning.app.snpaasadmin.pojo.CfServiceBought;
import com.suning.app.snpaasadmin.pojo.CfServiceBoughtExample;
import com.suning.app.snpaasadmin.pojo.CfUserRecord;
import com.suning.app.snpaasadmin.pojo.CfUserRecordExample;
import com.suning.app.snpaasadmin.util.Constants;
import com.suning.app.snpaasadmin.util.DbContextHolder;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserManagementDBOperation {
    
    @Autowired
    private CfUserExternalMapper userExMapper;
    
    @Autowired
    private AppEnvironmentMapper environmentMapper;
    
    @Autowired
    private CfAppMapper cfAppMapper;
    
    @Autowired
    private CfServiceBoughtMapper serviceBoughtMapper;
    
    @Autowired
    private CfSdkMapper cfSdkMapper;
    
    @Autowired
    private CfRuntimeMapper cfRuntimeMapper;
    
    @Autowired
    private CfServiceMapper cfServiceMapper;
    
    @Autowired
    private CfUserRecordMapper cfUserRecordMapper;

	public List<UserInfo> getAppList() {
	    DbContextHolder.setDbType(Constants.DATASOURCE_SERVER);
		List<UserInfo> userList = new ArrayList<UserInfo>();
		
		//获取用户购买的环境的总磁盘和内存
		userList = userExMapper.getDiskAndMemory();
		
		//根据用户购买的环境获取总的应用数和服务量
		if( userList != null && userList.size() >0) {
		    for(UserInfo userInfo : userList) {
		        
		        //将得出的内存和磁盘量除以1024（单位是G）
		        DecimalFormat decimalFormat=new DecimalFormat(".00");
		        String disk = decimalFormat.format(userInfo.getDisk()/1024);
		        String mem = decimalFormat.format(userInfo.getMemory()/1024);
		        userInfo.setDisk(Float.valueOf(disk));
		        userInfo.setMemory(Float.valueOf(mem));
		        //查询用户的应用数 和 服务数
		        UserInfo service = userExMapper.getServiceCount(userInfo.getUserId());
		        userInfo.setService(service.getService());
		        UserInfo app = userExMapper.getAppCount(userInfo.getUserId());
		        userInfo.setApp(app.getApp());
		    }
		}
		return userList;
	}
	
	public List<AppEnvironment> getUserAllEnv(String userId) throws Exception {
	    DbContextHolder.setDbType(Constants.DATASOURCE_SERVER);
	    List<AppEnvironment> envList = new ArrayList<>();
	    AppEnvironmentExample envExample = new AppEnvironmentExample();
	    envExample.createCriteria().andUserIdEqualTo(Long.valueOf(userId)).andIspaiedEqualTo(true);
	    envList = environmentMapper.selectByExample(envExample);
	    return envList;
	}
	
	public List<CfApp> getAppEnv(Integer envId) throws Exception {
	    DbContextHolder.setDbType(Constants.DATASOURCE_SERVER);
        List<CfApp> appList = new ArrayList<>();
        CfAppExample example = new CfAppExample();
        example.createCriteria().andEnvIdEqualTo(envId);
        appList = cfAppMapper.selectByExample(example);
        return appList;
	}
	
	public List<CfServiceBought> getServiceBoughts(Integer envId, Long userId) throws Exception {
	    DbContextHolder.setDbType(Constants.DATASOURCE_SERVER);
	    List<CfServiceBought> serviceBoughts= new ArrayList<>();
	    CfServiceBoughtExample example = new CfServiceBoughtExample();
	    example.createCriteria().andEnvIdEqualTo(envId).andUserIdEqualTo(userId);
	    serviceBoughts = serviceBoughtMapper.selectByExample(example);
	    return serviceBoughts;
	}
	
	public String getSdkName(Integer sdkId) throws Exception {
	    DbContextHolder.setDbType(Constants.DATASOURCE_SERVER);
	    CfSdk cfSdk = new CfSdk();
	    cfSdk = cfSdkMapper.selectByPrimaryKey(sdkId);
	    return cfSdk.getEnvName();
	}
	
	public String getRuntime(Integer runTimeId) throws Exception {
	    DbContextHolder.setDbType(Constants.DATASOURCE_SERVER);
	    CfRuntime runtime = new CfRuntime();
	    runtime = cfRuntimeMapper.selectByPrimaryKey(runTimeId);
	    return runtime.getRuntimeName();
	}
	
	public List<ServiceImgInfo> getServiceImg(Integer envId) throws Exception {
	    DbContextHolder.setDbType(Constants.DATASOURCE_SERVER);
	    List<ServiceImgInfo> serviceImgInfos = new ArrayList<>();
	    
	    //根据envId查找出所购买的service
	    CfServiceBoughtExample example = new CfServiceBoughtExample();
	    example.createCriteria().andEnvIdEqualTo(envId);
	    List<CfServiceBought> cfServiceBoughts = serviceBoughtMapper.selectByExample(example);
	    
	    //根据serviceId查找出img
	    if(cfServiceBoughts != null && cfServiceBoughts.size() > 0) {
	        for(CfServiceBought cfServiceBought : cfServiceBoughts) {
	            ServiceImgInfo serviceImgInfo = new ServiceImgInfo();
	            CfService cfService = cfServiceMapper.selectByPrimaryKey(cfServiceBought.getServiceId());
	            serviceImgInfo.setImg(cfService.getSerImg());
	            serviceImgInfo.setName(cfService.getSerName());
	            serviceImgInfos.add(serviceImgInfo);
	        }
	        return serviceImgInfos;
	    }
	    else {
	        return null;
	    }
	}
	
	public List<CfUserRecord> getUserRecordLog(String userId) throws Exception {
	    DbContextHolder.setDbType(Constants.DATASOURCE_SERVER);
	    CfUserRecordExample example = new CfUserRecordExample();
	    example.createCriteria().andUserIdEqualTo(Long.valueOf(userId));
	    List<CfUserRecord> userRecords = cfUserRecordMapper.selectByExample(example);
	    return userRecords;
	}
	
	public CfApp getOneApp(String appId) throws Exception {
	    DbContextHolder.setDbType(Constants.DATASOURCE_SERVER);
	    CfApp app = cfAppMapper.selectByPrimaryKey(Integer.valueOf(appId));
	    return app;
	}

}
